package com.hxs.learn.action.core;

import com.hxs.learn.constants.core.web.ReqConst;
import com.hxs.learn.constants.core.web.view.ViewNames;
import com.hxs.learn.entity.core.DepartmentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.hxs.learn.common.page.Page;
import com.hxs.learn.common.page.PageWrapper;
import com.hxs.learn.common.result.DataTableResult;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.common.web.annotation.Login;
import com.hxs.learn.entity.core.SysUser;
import com.hxs.learn.service.core.SysUserService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

/**
 * @author huxinsheng
 */
@RestController
@RequestMapping("core/user")
public class UserAction extends AbstractViewAction {
    private final SysUserService userService;

    @Autowired
    public UserAction(SysUserService userService) {
        this.userService = userService;
    }

    @GetMapping(ReqConst.REQ_INDEX)
    public ModelAndView index(@Login LoginInfo userInfo) {
        return createModelAndView(userInfo);
    }

    @GetMapping(ReqConst.REQ_USER_INFO)
    public ModelAndView userInfo(@Login LoginInfo userInfo, Model model) {
        SysUser sysUser = userService.findById(userInfo.getUserId());
        model.addAttribute(PAGE_DATA, sysUser);
        ModelAndView mv = new ModelAndView();
        mv.setViewName(ViewNames.PAGE_USER_INFO);
        return createModelAndView(mv);
    }

    @GetMapping(ReqConst.REQ_USER_PASSWORD)
    public ModelAndView userPwd(@Login LoginInfo userInfo) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName(ViewNames.PAGE_USER_PASSWORD);
        return createModelAndView(mv);
    }

    @ApiOperation(
            value = "保存用户数据",
            notes = "用户管理时保存用户数据",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    @PostMapping(ReqConst.REQ_MODIFY_DATA)
    @ResponseBody
    public Result modifyData(@Login LoginInfo loginInfo, SysUser userPojo) {
        return userService.save(loginInfo, userPojo);
    }

    @ApiOperation(
            value = "编辑用户页面",
            notes = "用户管理时显示编辑用户页面"
    )
    @ApiImplicitParams({@ApiImplicitParam(
            name = "id",
            value = "权限ID",
            dataType = "String",
            paramType = "query"
    ),})
    @GetMapping(ReqConst.REQ_MODIFY_DATA_DIALOG)
    public ModelAndView modifyDataDialog(@Login LoginInfo loginInfo, Model model, String id) {
        if (!StringUtils.isEmpty(id)) {
            SysUser userPojo = userService.findById(id);
            model.addAttribute(PAGE_DATA, userPojo);
        }
        DepartmentInfo departmentInfo = new DepartmentInfo();
        departmentInfo.setPid(loginInfo.getHospitalId());
        return createModelAndView(ViewNames.PAGE_USER_MODIFY);
    }

    @ApiOperation(
            value = "删除用户数据",
            notes = "用户管理时删除用户数据"
    )
    @PostMapping(ReqConst.REQ_DELETE_DATA_BY_ID)
    @ResponseBody
    public Result modifyStatusData(@Login LoginInfo loginInfo, @PathVariable String id) {
        return userService.removeById(loginInfo, id);
    }

    @ApiOperation(
            value = "修改用户状态数据",
            notes = "用户管理时修改用户状态数据"
    )
    @PostMapping(ReqConst.REQ_MODIFY_STATUS_DATA)
    @ResponseBody
    public Result modifyStatusData(@Login LoginInfo loginInfo, String id, Integer status) {
        return userService.modifyUserStatus(loginInfo, id, status);
    }

    @ApiOperation(
            value = "获取用户页面数据",
            notes = "用户管理时获取用户页面数据"
    )
    @ApiImplicitParams({@ApiImplicitParam(
            name = "keyword",
            value = "搜索关键字(登录用户名/用户名称)",
            dataType = "String",
            paramType = "query"
    ),})
    @ResponseBody
    @GetMapping(ReqConst.REQ_QUERY_PAGE_DATA)
    public Result queryPageData(Page page, String keyword) {
        PageWrapper<SysUser> wrapper = userService.findSysUserPageData(page, keyword);

        return DataTableResult.ok(wrapper.getPage(), wrapper.getRows());
    }

    @ApiOperation(
            value = "编辑用户页面",
            notes = "用户管理时显示编辑用户页面"
    )
    @ApiImplicitParams({@ApiImplicitParam(
            name = "id",
            value = "权限ID",
            dataType = "String",
            paramType = "query"
    ),})
    @RequestMapping(value = ReqConst.REQ_MODIFY_PASSWORD, method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object modifyPassword(@Login LoginInfo loginInfo, SysUser sysUser, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView(ViewNames.PAGE_USER_PASSWRD_MODIFY);
        if (ReqConst.REQ_METHOD_POST.equals(request.getMethod())) {
            try {
                return userService.modifyPwd(loginInfo, sysUser);
            } catch (Exception e) {
                return Result.error(e.getMessage());
            }
        }
        return mv;
    }

    @ApiOperation(
            value = "重置用户密码",
            notes = "重置用户密码"
    )
    @ResponseBody
    @PostMapping(ReqConst.REQ_RESET_PASSWORD)
    public Result resetPwd(SysUser sysUser) {
        return userService.resetPwd(sysUser);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
